<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class MenuModel extends Model
{
    use ValidatorModel;

    protected $table = 'menu';

    public static $rules = [
        'name'=>'required|alpha_num|max:10|min:2',
        'pid' => 'required|integer',
        'url' => '',
        'is_show' => 'required|integer',
        'sort' => 'integer',
    ];


    public function lists(array $data)
    {
        $condition = [];

        if(isset($data['name'])){
            $condition[] = ['name', 'like', "%{$data['name']}%"];
        }

        if(isset($data['is_show'])){
            $condition[] = ['is_show', $data['is_show']];
        }

        $start = start($data['page'], $data['limit']);
        $res = $this->where($condition)->offset($start)->limit($data['limit'])->orderBy('id', 'desc')->get();

        $count = $this->where($condition)->count();

        return ['code' => 0, 'count' => $count, 'data' => $res, 'msg' => ''];
    }


    public function find($condition, $field)
    {
        return $this->where($condition)->select()->first()->toArray();
    }


    public function create($data)
    {
        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        $data['is_show'] = isset($data['is_show']) ? 1 : 0;
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');

        $rules = $this->getRules($data);

        $error = $this->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }

        $this->insert($data);
    }


    public function edit($id , $data)
    {
        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        if($id == $data['pid']){
            throw new \Exception('不能选择本身!');
        }

        $data['is_show'] = isset($data['is_show']) ? 1 : 0;
        $data['updated_at'] = date('Y-m-d H:i:s');

        $condition = ['id'=>$id];

        $this->saveMenu($condition, $data);
    }


    public function del($ids)
    {
        return static::destroy($ids);
    }


    public function saveMenu(array $condition , array $data)
    {
        $rules = $this->getRules($data);

        $error = $this->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }

        $this->where($condition)->update($data);
    }

    public function menu()
    {
        $topMenus = $this->where([
            'pid'=>0,
            'is_show'=>1
        ])->orderBy('sort')->get();

        foreach ($topMenus as $menu){
            $menu['child'] = $this->where([
                'pid'=>$menu['id'],
                'is_show'=>1
            ])->orderBy('sort')->get();
        }

        return $topMenus;
    }


    public function normMenu($pidName = 'pid',$parent_id=0,$level=0){
        $res=array();
        static $arr;
        $arr = $arr ? $arr : $this->all();
        foreach($arr as $v){
            if($v[$pidName]==$parent_id){
                $v['disabled']=false;
                foreach($arr as $v1){
                    if($v1[$pidName]==$v['id']){
                        $v['disabled']=true;
                        break;
                    }
                }

                $v['level']=$level;
                $res[]=$v;
                $res=array_merge($res,$this->normMenu($pidName,$v['id'],$level+1));
            }
        }
        return $res;
    }
}